package com.itboye.sunsun.shop.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.itboye.sunsun.R;
import com.itboye.sunsun.adapter.GouWuZiXunAdapter;
import com.itboye.sunsun.beans.GouWuZiXunBean;
import com.itboye.sunsun.beans.GouWuZiXunBean.GetData.GetList;
import com.itboye.sunsun.beans.NewsHotReaBean;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class GouMaiZiXunActivity extends BaseActivity implements IXListViewListener {

	private String goodId;
	private XListView xlistview;
	private ArrayList<GetList> arrayList;
	private GouWuZiXunAdapter adapter;
	private int page_no=1;
	private int type;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);

		setContentView(R.layout.activity_goumai_zixun);

		findViewById(R.id.back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		findViewById(R.id.zixun).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent=new Intent(getApplicationContext(),
						WoYaoZiXunActivity.class);
				intent.putExtra("goodsId", goodId);
				startActivity(intent);
			}
		});
		initData();
//		getData(1);//1为下拉，0为上拉
	}

	private void initData() {
		arrayList = new ArrayList<GouWuZiXunBean.GetData.GetList>();
		adapter = new GouWuZiXunAdapter(arrayList, GouMaiZiXunActivity.this);
		xlistview.setAdapter(adapter);
		xlistview.setXListViewListener(this);
		Intent intent = getIntent();
		goodId = intent.getStringExtra("goodid");
	}

	private void getData( int pullOrPush) {
		 type=pullOrPush;
		MyJsonRequest<GouWuZiXunBean.GetData> request = new MyJsonRequest.Builder<GouWuZiXunBean.GetData>()
				.apiVer("100")
				.param("pid", goodId)
				.param("page_no", page_no)
				.typeKey("BY_Product_queryFaq")
				.requestListener(
						new XRequestListener<GouWuZiXunBean.GetData>() {

							@Override
							public void onResponse(GouWuZiXunBean.GetData arg0) {
								stopRefresh();
								DebugLog.v("goods", arg0.getList());
								if (arg0.getList() != null) {
									findViewById(R.id.txt_nodata).setVisibility(View.GONE);
									if(type==1)arrayList.clear();
										arrayList.addAll(arg0.getList());
									adapter.notifyDataSetChanged();
								}else{
									findViewById(R.id.txt_nodata).setVisibility(View.VISIBLE);
								}
							}
						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		getData(1);
		super.onResume();
	}
	
	@Override
	public void onRefresh() {
		page_no=1;
		getData(1);
	}

	@Override
	public void onLoadMore() {
		page_no++;
		getData(0);
	}

	public void stopRefresh(){
		xlistview.stopLoadMore();
		xlistview.stopRefresh();
	}
	
}
